package com.zyq.shoppinglist;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.elv)
    ExpandableListView elv;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.sum_price)
    TextView sumPrice;
    @BindView(R.id.js)
    TextView js;
    private ShopAdapter shopAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        getdates();

        //全选
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    setcheckAll(1);
                } else {
                    setcheckAll(0);
                }
            }
        });

        //跳转到订单页面
        js.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startActivity(new Intent(MainActivity.this, OrderActivity.class));
            }
        });
    }

    private void getdates() {
        List<ShopEntity.DataBean> dataBeans = new ArrayList<ShopEntity.DataBean>();
        ShopEntity.DataBean.ListBean listBean1 = new ShopEntity.DataBean.ListBean();
        listBean1.setPrice(100);
        listBean1.setNum(10);
        listBean1.setTitle("这是手机华为的");
        listBean1.setChecked(false);
        ShopEntity.DataBean.ListBean listBean2 = new ShopEntity.DataBean.ListBean();
        listBean2.setPrice(200);
        listBean2.setNum(20);
        listBean2.setTitle("这是手机苹果的");
        listBean2.setChecked(false);

        List<ShopEntity.DataBean.ListBean> list = new ArrayList<ShopEntity.DataBean.ListBean>();
        list.add(listBean1);
        list.add(listBean2);
        ShopEntity.DataBean dataBean1 = new ShopEntity.DataBean();
        dataBean1.setSellerName("商户1");
        dataBean1.setChecked(false);
        dataBean1.setList(list);


        ShopEntity.DataBean.ListBean listBean3 = new ShopEntity.DataBean.ListBean();
        listBean3.setPrice(100);
        listBean3.setNum(10);
        listBean3.setTitle("这是手机华为的");
        listBean3.setChecked(false);
        ShopEntity.DataBean.ListBean listBean4 = new ShopEntity.DataBean.ListBean();
        listBean4.setPrice(200);
        listBean4.setNum(20);
        listBean4.setTitle("这是手机苹果的");
        listBean4.setChecked(false);
        List<ShopEntity.DataBean.ListBean> list1 = new ArrayList<ShopEntity.DataBean.ListBean>();
        list1.add(listBean3);
        list1.add(listBean4);
        ShopEntity.DataBean dataBean2 = new ShopEntity.DataBean();
        dataBean2.setSellerName("商户2");
        dataBean2.setChecked(false);
        dataBean2.setList(list1);
        dataBeans.add(dataBean1);
        dataBeans.add(dataBean2);
        shopAdapter = new ShopAdapter(this, dataBeans);
        //调用
        initItemChange();
        elv.setAdapter(shopAdapter);
        for (int i = 0; i < dataBeans.size(); i++) {
            elv.expandGroup(i);
        }
    }

    //全选
    private void setcheckAll(int s) {
        int groupCount = shopAdapter.getGroupCount();
        for (int i = 0; i < groupCount; i++) {
            ShopEntity.DataBean group = (ShopEntity.DataBean) shopAdapter.getGroup(i);
            boolean checked = group.isChecked();
            if (!checked) {
                group.setChecked(true);
            } else {
                group.setChecked(false);
            }

            List<ShopEntity.DataBean.ListBean> list = group.getList();
            for (int j = 0; j < list.size(); j++) {
                list.get(j).setSelected(s);
            }
        }
        shopAdapter.notifyDataSetChanged();
        getTotal();
    }

    //计算总价
    public void getTotal() {
        double total = 0;
        List<ShopEntity.DataBean> list = shopAdapter.getList();
        for (int i = 0; i < list.size(); i++) {
            List<ShopEntity.DataBean.ListBean> list1 = list.get(i).getList();
            for (int j = 0; j < list1.size(); j++) {
                boolean checked = list1.get(j).isChecked();
                if (checked) {
                    double price = list1.get(j).getPrice();
                    int num = list1.get(j).getNum();
                    total += price * num;
                }
            }
        }

        sumPrice.setText("合计：" + total);
    }


    //点击条目进行变化
    public void initItemChange() {
        //点击父条目
        elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                ShopEntity.DataBean group = (ShopEntity.DataBean) shopAdapter.getGroup(groupPosition);
                group.setChecked(!group.isChecked());
                int cb = 0;
                if (group.isChecked()) {
                    cb = 1;
                }
                List<ShopEntity.DataBean.ListBean> list = group.getList();
                for (int i = 0; i < list.size(); i++) {
                    ShopEntity.DataBean.ListBean listBean = list.get(i);
                    listBean.setSelected(cb);
                }
                shopAdapter.notifyDataSetChanged();
                getTotal();
                return true;
            }
        });


        //点击子条目
        elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                ShopEntity.DataBean.ListBean child = (ShopEntity.DataBean.ListBean) shopAdapter.getChild(groupPosition, childPosition);
                boolean checked = child.isChecked();
                if (checked) {
                    child.setSelected(0);
                } else {
                    child.setSelected(1);
                }
                shopAdapter.notifyDataSetChanged();
                getTotal();
                return true;
            }
        });

        //点击加减器
        shopAdapter.setOnChagenum(new ShopAdapter.OnChagenum() {
            @Override
            public void change(View view, int num) {
                getTotal();
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        shopPresenter.destory();
    }
}


